package com.shangxia.lucky.lucky.writeHandler;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: monomer
 * @ClassName: MyWriteHandler
 * @Time: 2021/8/13
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class MyWriteHandler extends LongestMatchColumnWidthStyleStrategy implements CellWriteHandler, SheetWriteHandler {
    /**
     * 变色列
     */
    public static Map<Integer, List<Integer>> CHANGE_COLOR_MAP = new HashMap<>();
    public static CellStyle CELLSTYLE;
    public org.apache.poi.ss.usermodel.Sheet Sheet;
    public volatile Integer OUT_COLUMN;//正在输出的列
    public volatile Integer size;//正在输出的列


    public MyWriteHandler(Integer OUT_COLUMN) {
        this.OUT_COLUMN = OUT_COLUMN;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        if (Sheet == null) {
            Sheet = cell.getSheet();
            //设置行宽
            Sheet.setDefaultColumnWidth(3);
        }
        setCellRule(cell);
    }

    private void setCellRule(Cell cell) {
        if (cell.getRowIndex() > (1 + OUT_COLUMN) && cell.getRowIndex() < CHANGE_COLOR_MAP.size() + 2 + OUT_COLUMN) {
            if (null == CELLSTYLE) {
                Workbook workbook = cell.getSheet().getWorkbook();
                CELLSTYLE = workbook.createCellStyle();
                //设置前景填充样式
                CELLSTYLE.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                CELLSTYLE.setFillForegroundColor(IndexedColors.RED.getIndex());
            }

            try {
                if (CHANGE_COLOR_MAP.get(cell.getRowIndex() - 2).contains(cell.getColumnIndex())) {
                    cell.setCellStyle(CELLSTYLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.createFreezePane(0, 2, 1, 2);
    }
}
